package com.example.hellondk;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;



/**
 * @author hello
 * 
 *         
 */
public class Unicode2Chinese {
	
		public static String unicode2Chinese(String string){
			String[] strs = string.split(" ");
			StringBuilder sb = new StringBuilder();
			List<String> strs1 = new ArrayList<String>();
			List<String> strs2 = new ArrayList<String>();
			for (int i = 0; i < strs.length; i++) {
				if (i%2 == 0) {
					strs1.add(strs[i]);
				}else {
					strs2.add(strs[i]);
				}
			}
			for (int i = 0; i < strs2.size(); i++) {
				sb.append("\\u");
				sb.append(strs2.get(i))
				.append(strs1.get(i));
			}
			return decode(sb.toString());
		}

		public static String decode(String in) {
			try {
				return decode(in.toCharArray());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return in;
		}

		private static String decode(char[] in) throws Exception {
			int off = 0;
			char c;
			char[] out = new char[in.length];
			int outLen = 0;
			while (off < in.length) {
				c = in[off++];
				if (c == '\\') {
					if (in.length > off) { // 是否有下一个字符
						c = in[off++]; // 取出下一个字符
					} else {
						out[outLen++] = '\\'; // 末字符为'\'，返回
						break;
					}
					if (c == 'u') { // 如果是"\\u"
						int value = 0;
						if (in.length >= off + 4) { // 判断"\\u"后边是否有四个字符
							boolean isUnicode = true;
							for (int i = 0; i < 4; i++) { // 遍历四个字符
								c = in[off++];
								switch (c) {
								case '0':
								case '1':
								case '2':
								case '3':
								case '4':
								case '5':
								case '6':
								case '7':
								case '8':
								case '9':
									value = (value << 4) + c - '0';
									break;
								case 'a':
								case 'b':
								case 'c':
								case 'd':
								case 'e':
								case 'f':
									value = (value << 4) + 10 + c - 'a';
									break;
								case 'A':
								case 'B':
								case 'C':
								case 'D':
								case 'E':
								case 'F':
									value = (value << 4) + 10 + c - 'A';
									break;
								default:
									isUnicode = false; // 判断是否为unicode码
								}
							}
							if (isUnicode) { // 是unicode码转换为字符
								out[outLen++] = (char) value;
							} else {  // 不是unicode码把"\\uXXXX"填入返回值
								off = off - 4;
								out[outLen++] = '\\';
								out[outLen++] = 'u';
								out[outLen++] = in[off++];
								Log.i(IDCardReadActivity.TAG, "不是unicode码");
							}
						} else { // 不够四个字符则把"\\u"放入返回结果并继续
							out[outLen++] = '\\';
							out[outLen++] = 'u';
							Log.i(IDCardReadActivity.TAG, "字符不够");
							continue;
						}
					} else {
						switch (c) { // 判断"\\"后边是否接特殊字符，回车，tab一类的
						case 't':
							c = '\t';
							out[outLen++] = c;
							break;
						case 'r':
							c = '\r';
							out[outLen++] = c;
							break;
						case 'n':
							c = '\n';
							out[outLen++] = c;
							break;
						case 'f':
							c = '\f';
							out[outLen++] = c;
							break;
						default:
							out[outLen++] = '\\';
							out[outLen++] = c;
							Log.i(IDCardReadActivity.TAG, "特殊字符");
							break;
						}
					}
				} else {
					out[outLen++] = (char) c;
				}
			}
			return new String(out, 0, outLen);
		}
}
